function selectpv(pid, vid) {
	// 如果点击的是无效的或者以选中的属性值,则直接返回
	var eleid = "#pv_" + pid + "\\:" + vid;
	var className = $(eleid).attr('class');
	if (className == 'item saleOut') {
		return;
	}
			
	if (className == 'item selected') {
		$(eleid).attr('class', 'item');
		$('#seleted_pv_' + pid).val('');
	} else {
		// 1 遍历同类属性的属性值,将同类的已选中的属性值样式改变成普通的,将点击的属性值样式改成选中的
		$("div[id^='pv_" + pid + ":']").each(function(i){
			var className = $($("div[id^='pv_" + pid + ":']")[i]).attr('class');
			var id = $($("div[id^='pv_" + pid + ":']")[i]).attr('id').replace('pv_', '');
			if (className == 'item selected') { //已选中的属性值样式改变成普通的
				$($("div[id^='pv_" + pid + ":']")[i]).attr('class', 'item');
			}
						
			if (id == (pid + ":" + vid)) { //点击的属性值样式改成选中
				$($("div[id^='pv_" + pid + ":']")[i]).attr('class', 'item selected');
			}
		});
		$('#seleted_pv_' + pid).val(vid);
	}
			
	var pids = $('#pids').val();
	var pid_array = pids.split(',');
	for (var i = 0; i < pid_array.length; ++i) {
		if (pid_array[i] != pid) {
			show(pid_array[i], pid_array);
		}
	}
	setSkuID();
}
	
// 根据其他属性的选中情况, 显示某一个属性的可选属性值
function show(pid, pid_array) {
	var otherSelected = new Array(); // 除去该pid之外其他属性所选中的属性值
	for (var i = 0; i < pid_array.length; ++i) {
		if (pid_array[i] != pid) {
			var vid = $('#seleted_pv_' + pid_array[i]).val();
			if (vid != '') {
				otherSelected.push(pid_array[i] + ":" + vid);
			}
		}
	}
	
	// 如果其他属性一个都没选中,则该属性的属性值(除了以选中的之外)全部变为可选
	if (otherSelected.length == 0) {
		$("div[id^='pv_" + pid + ":']").each(function(i){
			var className = $($("div[id^='pv_" + pid + ":']")[i]).attr('class');
			if (className == 'item saleOut') {
				$($("div[id^='pv_" + pid + ":']")[i]).attr('class', 'item');
			}
		});
		
		return;
	}
	
	$("div[id^='pv_" + pid + ":']").each(function(i){
		var className = $($("div[id^='pv_" + pid + ":']")[i]).attr('class');
		var id = $($("div[id^='pv_" + pid + ":']")[i]).attr('id').replace('pv_', '');
		if (className != 'item selected') {
			$($("div[id^='pv_" + pid + ":']")[i]).attr('class', 'item saleOut');
		
			$("[id^=sku_data_]").each(function(j){
				var prop = $($("[id^=sku_data_]")[j]).attr('id').replace('sku_data_', '') + ","; // 获取当前遍历到的sku的属性properties
				var price = $($("[id^=sku_data_]")[j]).attr('price');
				var stock = $($("[id^=sku_data_]")[j]).attr('stock');
				
				var containThisProp = prop.indexOf(id + ",") != -1;
				var containOtherProp = true;
				for (var k = 0; k < otherSelected.length; ++k) {
					if (prop.indexOf(otherSelected[k] + ",") == -1 || stock == '0') {
						containOtherProp = false;
					}
				}
				
				if (containThisProp && containOtherProp) {
					$($("div[id^='pv_" + pid + ":']")[i]).attr('class', 'item');
				}
			});
		}
		
	});
}
	
function setSkuID() {
	var pids = $('#pids').val();
	var pid_array = pids.split(',');
	
	var selected_pv = new Array();
	for (var i = 0; i < pid_array.length; ++i) {
		var vid = $('#seleted_pv_' + pid_array[i]).val();
		if (vid != '') {
			selected_pv.push(pid_array[i] + ":" + vid);
		} else {
			//$('#curStock').html($('#total_stock').val());
			$('#skuID').val('');
			return;
		}
	}
	
	$("[id^=sku_data_]").each(function(j){
		var prop = $($("[id^=sku_data_]")[j]).attr('id').replace('sku_data_', '') + ","; // 获取当前遍历到的sku的属性properties
		var skuID = $($("[id^=sku_data_]")[j]).attr('skuID');
		var price = $($("[id^=sku_data_]")[j]).attr('price');
		var stock = $($("[id^=sku_data_]")[j]).attr('stock');
		
		for (var i = 0; i < selected_pv.length; ++i) {
			prop = prop.replace(selected_pv[i] + ",", '');
		}
		if (prop == '') {
			selectSkuID(skuID);
			
			return false;
		} else {
			$('#skuID').val('');
			//$('#curStock').html($('#total_stock').val());
		}
	});
}

function selectSkuID(skuID) {
	var isValidScore = $('#isValidScore').val();
	var discountFlag = $("#discountFlag").val();
	var groupBuyFlag = $("#groupBuyStock").val() != '';
	
	
	var stock = $("[skuID=" + skuID + "]").attr('stock');
	var price = $("[skuID=" + skuID + "]").attr('price');
	
	//如果该商品是团购商品，则商品的库存为团购商品的促销数量，售价为团购价
	if (groupBuyFlag) {
		stock = $("#groupBuyStock").val();
		price = $("#groupBuyPrice").val();
	}
	
	//如果该商品是特价折扣商品，则商品的售价是折扣后的价格，商品原价是原商品的价格，优惠金额=商品原价-折扣后的价格
	if(discountFlag == "1") {
		var rate = $("#discountRate").val();
		var rateFloat = parseFloat(rate);
		if(rateFloat < 0) {
			return false;
		}
		var tmpPrice = rateFloat / 10.0 * price;
		if (tmpPrice < 0.01) {
			tmpPrice = 0.00;
		}
		tmpPrice = tmpPrice.toFixed(2);
		
		oldPrice = price;
		price = tmpPrice;
		
		var discountAmount = oldPrice - price;
		$("#oldProductPrice").html(currencyValue + oldPrice);
		$("#discountAmount").html(discountAmount.toFixed(2));
	}
	
	$('#skuID').val(skuID);
	$('#curStock').html(stock);//显示库存量
	
	//显示价格,加入购物车按钮
	if (price == '0.00') {
		$('#givePoint_span').html('0');
		if (groupBuyFlag == '1') {
			$('#productPrice').html(currencyValue + '0.00');
			$('#addCart_a').html('加入购物车');
			$('#addCart_disable_a').html('加入购物车');
			$('#addCart_f').html('免费领取');
			$('#addCart_disable_f').html('免费领取');
			$('#addCart_a').show();
			$('#addCart_disable_a').hide();
			$('#addCart_f').show();
			$('#addCart_disable_f').hide();
		} else {
			$('#productPrice').html('面议');
			$('#addCart_a').html('加入购物车');
			$('#addCart_disable_a').html('加入购物车');
			$('#addCart_f').html('立即购买');
			$('#addCart_disable_f').html('立即购买');
			$('#addCart_a').hide();
			$('#addCart_disable_a').show();
			$('#addCart_f').hide();
			$('#addCart_disable_f').show();
		}
	} else {
		$('#givePoint_span').html(parseInt(price));
		$('#productPrice').html(currencyValue + price);
		$('#addCart_a').html('加入购物车');
		$('#addCart_disable_a').html('加入购物车');
		$('#addCart_f').html('立即购买');
		$('#addCart_disable_f').html('立即购买');
		$('#addCart_a').show();
		$('#addCart_disable_a').hide();
		$('#addCart_f').show();
		$('#addCart_disable_f').hide();
	}
	
	//显示积分兑换按钮
	if (isValidScore != '1') {
		$('#addCart_e').hide();
		$('#addCart_disable_e').hide();
	} else {
		if (stock > 0) {
			$('#addCart_e').show();
			$('#addCart_disable_e').hide();
		} else {
			$('#addCart_e').hide();
			$('#addCart_disable_e').show();
		}
	}
	
	//如果sku库存小于等于0，则“加入购物车/免费领取”，“立即购买”，“积分兑换”按钮全部置灰；其中如果开启了零元购，并且sku价格为0.00，则立即购买按钮都不显示
	if (stock <= 0) {
		$('#addCart_a').hide();
		$('#addCart_f').hide();
		$('#addCart_e').hide();
		$('#addCart_disable_a').show();
		$('#addCart_disable_f').show();
		$('#addCart_disable_e').show();
	}
	
	
	return false;
}
	
function checkSku() {
	var skuID = $('#skuID').val();
	if (skuID == '') {
//		$('#addCart_a').hide();
		$('#msg_msg_buy').html('请选择购买参数');
		$('#msg_msg_buy').show();
		location.hash = '#productSKU';
		return false;
	}
	
	return true;
}